package tw.edu.tku.im.maze;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class StartGameRep extends RepPacket {

   Member[] team1;
   Member[] team2;
   MazeMap map;
   
 //-------------------------------------------------------
   public StartGameRep(Member[] team1, MazeMap map) {
      this(team1, null, map);
   }
   
   public StartGameRep(Member[] team1, Member[] team2, MazeMap map) {
      this.team1 = team1;
      this.team2 = team2;
      this.map = map;
   }
   
   //-------------------------------------------------------
   public void setTeam1(Member[] team) {
      this.team1 = team;
   }
   public Member[] getTeam1() {
      return this.team1;
   }
   
   //-------------------------------------------------------
   public void setTeam2(Member[] team) {
      this.team2 = team;
   }
   public Member[] getTeam2() {
      return this.team2;
   }
   
   //-------------------------------------------------------
   public void setMap(MazeMap map) {
      this.map = map;
   }
   public MazeMap getMap() {
      return this.map;
   }
   
   //-------------------------------------------------------
   public String toJSONString() throws JSONException {
      JSONArray team1 = new JSONArray();
      JSONObject result = new JSONObject();
      result.put("team1", team1);
      for(Member member : this.team1) {
         if(member==null) { break; }
         team1.put(new JSONObject(member.toJSONString()));
      }
      if(this.team2!=null) {
         JSONArray team2 = new JSONArray();
         for(Member member : this.team2) {
            if(member==null) { break; }
            team2.put(new JSONObject(member.toJSONString()));
         }
         if(this.team2[0]!=null) {
            result.put("team2", team2); 
         }
      }
      result.put("map", new JSONObject(this.map.toJSONString()));      
      JSONObject jsonObj = new JSONObject();
      jsonObj.put("result", result);
      jsonObj.put("action", "startGameResponse");
      return jsonObj.toString();
   }

   @Override
   public String getPacketName() {
      return "startGameResponse";
   }

   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel arg0, int arg1) {
      
   }

}
